<?php namespace Common\Model;

/*
 * 
 *员工模型 
 * */
 use \Hdphp\Model\Model;
 class staff extends Model
 {
 	protected $table = "staff";//连接表
	/*
	 * 自动验证
	 * */
	protected $validate =array(
		array("staff_name","required","请输入员工姓名",1,3),
		array("staff_name","china","请输入正确的用户名",1,3),
		array("staff_account","required","请输入员工账号",3,3),
		array("staff_account","minlen:2","请输2-20位账号",3,3),
		array("staff_account","maxlen:20","请输2-20位账号",3,3),
	);
	//登录
 	public function login()
	{
		if(!$this->create()) return FALSE;
		
		//p($_POST);die;
		//验证用户名密码是否匹配
		$uname =$this->where("staff_account",$_POST['staff_account'])->first();
		//var_dump($uname);die;
		if(!$uname){
			$this->error="用户名不存在";
			return FALSE;
		}
		if($uname["password"]!=md5($_POST["password"])){
			$this->error="密码不正确";
			return FALSE;
		}
		//存session
		$_SESSION['sid']=$uname['sid'];
		$_SESSION['staff_name'] = $uname['staff_name'];
		return TRUE;
	}
	//退出
	public function outlog()
	{
		session_unset();
		session_destroy();
		return TRUE;
	}
	
	
	//添加员工
	public function addstaff()
	{
		//自动验证
		if(!$this->create()) return FALSE;
		//判断帐号是否存在
		$data = $this->where("staff_account",$_POST["staff_account"])->first();
		if($data){
			$this->error = "帐号已存在";
			return FALSE;
		}
		$password = md5($_POST['password']);
		$_POST['password'] = $password;
		//添加到数据库
		return  $this->add($_POST);
		
	}
	//编辑员工
	public function resetstaff()
	{
		if(!$this->create()) return FALSE;
		//判断帐号是否存在
		$data = $this->where("staff_account",$_POST["staff_account"])->first();
		if($data){
			$this->error = "帐号已存在";
			return FALSE;
		}
		$_POST['password'] = md5($_POST['password']);
		//修改数据库
		return $this->save();
	}
	//删除
	public function del($sid)
	{
		$this->where("sid",$sid)->delete();
		return TRUE;
	}
 }
